package assdraw2.canvas.abstraction;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Polygon;
import java.lang.Math;

import assdraw2.canvas.presentation.Visitor;




public class Line extends Shape{

	private double endX, endY;
	private static final double MARGIN = 3;

	
	public Line(double x, double y, double endX, double endY, Color c){
		super(x,y,c);
		this.endX = endX;
		this.endY = endY;
	}

	@Override
	public void scale(double fx, double fy) {
		endX = getX() + (endX - getX()) * fx;
		endY = getY() + (endY - getY()) * fy;
	}

	public void set(double endX, double endY) {
		this.endX = endX;
		this.endY = endY;
	}


	public double getEndX() {
		return endX;
	}
	
	public double getEndY() {
		return endY;		
	}
	
	private double getUnitdX() {
		return Math.signum(endX - this.getX());
	}
	
	private double getUnitdY() {
		return Math.signum(endY - this.getY());
	}

	@Override
	public boolean contains(double x2, double y2) {
		Polygon p = new Polygon();
		p.addPoint((int)(getX() - getUnitdY()*MARGIN), (int)(getY() + getUnitdX()*MARGIN));
		p.addPoint((int)(getEndX() - getUnitdY()*MARGIN), (int)(getEndY() + getUnitdX()*MARGIN));
		p.addPoint((int)(getEndX() + getUnitdY()*MARGIN), (int)(getEndY() - getUnitdX()*MARGIN));
		p.addPoint((int)(getX() + getUnitdY()*MARGIN), (int)(getY() - getUnitdX()*MARGIN));
		return p.contains(x2, y2);
	}

	@Override
	public void accept(Visitor v) {
		v.visit(this);
	}

	@Override
	public Rectangle getBoundary() {
		double rx = Math.min(getX(), endX);
		double ry = Math.min(getY(), endY);
		double h = Math.abs(getY() - endY);
		double w = Math.abs(getX() - endX);
		return new Rectangle((int)rx, (int)ry, (int)w, (int)h);
	}
	
	@Override
	public void move(double x, double y){
		double deltaX = x - getX();
		double deltaY = y - getY();
		setX(x);
		setY(y);
		endX += deltaX;
		endY += deltaY;
	}

	
}
